import 'dart:io';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:english_words/english_words.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:my_app/app/core/utils/url_util.dart';
import 'package:my_app/app/core/theme/app_colors.dart';
import 'package:my_app/app/widgets/image_preview_gallery_widget.dart';

class CustomImageWidget extends StatelessWidget {
  //图片宽
  final double? width;

  //图片高
  final double? height;

  //图片
  final String imageUrl;

  ///图片缩放方式
  final BoxFit? fit;

  ///是否为圆形组件
  final bool isCircle;

  ///Tag 用于Hero
  final String? tag;

  ///用于画廊
  final List<ImageGalleryEntity>? images;

  ///画廊当前index
  final int? curIndex;

  ///圆角
  final double? radius;

  ///是否开启预览
  final bool isPreview;

  const CustomImageWidget({
    super.key,
    this.width,
    this.height,
    required this.imageUrl,
    this.fit = BoxFit.cover,
    this.isCircle = false,
    this.tag,
    this.images,
    this.curIndex,
    this.radius,
    this.isPreview = true,
  });

  @override
  Widget build(BuildContext context) {
    final String str = WordPair.random().toString();
    return Hero(
      tag: tag ?? str,
      child: ClipRRect(
        borderRadius: BorderRadius.circular(isCircle ? 100 : radius ?? 0),
        child: isPreview
            ? GestureDetector(
                onTap: () => Get.to(
                  PhotoPreviewGalleryWidget(
                    images: images ??
                        [
                          ImageGalleryEntity(
                            url: imageUrl,
                            tag: tag ?? str,
                          )
                        ],
                    index: curIndex ?? 0,
                  ),
                ),
                child: buildImageContainer(),
              )
            : buildImageContainer(),
      ),
    );
  }

  Widget buildImageContainer() {
    return Container(
      color: Color(0xFFf4f5f7),
      child: buildImageWidget(),
    );
  }

  Widget buildImageWidget() {
    if (isRemote(imageUrl)) {
      return CachedNetworkImage(
        imageUrl: imageUrl,
        placeholder: (cxt, url) =>
            Center(child: Image.asset('assets/images/loading.gif', width: 30)),
        // image: image,
        width: width,
        height: height,
        fit: fit,
        errorWidget: (context, error, stackTrace) => errorWidget(),
      );
    }
    if (isFileUrl(imageUrl)) {
      return FadeInImage(
        width: width,
        height: height,
        fit: fit,
        placeholder: AssetImage('assets/images/loading.gif'),
        image: FileImage(File(imageUrl)),
        imageErrorBuilder: (context, error, stackTrace) => errorWidget(),
      );
    }

    return FadeInImage(
      width: width,
      height: height,
      fit: fit,
      placeholder: AssetImage('assets/images/loading.gif'),
      image: AssetImage(imageUrl),
      imageErrorBuilder: (context, error, stackTrace) => errorWidget(),
    );
  }

  Widget errorWidget() {
    return Container(
        width: width,
        height: height,
        decoration: BoxDecoration(
            color: imageErrorBg, borderRadius: BorderRadius.circular(5.0)),
        child: Center(
            child: Icon(
          Icons.error_outline_outlined,
          color: textTipsColor,
          size: 16.0,
        )));
  }
}
